package com.psp.bu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.northend.util.StringUtil;
import com.northend.util.VCodeSender;
import com.psp.bu.constants.StringConstants;
import com.psp.bu.controller.res.bean.RDemandBean;
import com.psp.bu.controller.res.bean.RParkUserBean;
import com.psp.bu.model.DemandBean;
import com.psp.bu.model.ParkUserBean;
import com.psp.bu.model.UserBean;
import com.psp.bu.persist.dao.DemandDao;
import com.psp.bu.persist.dao.UserDao;
import com.psp.bu.service.DemandService;
import com.psp.bu.service.exception.ServiceException;

@Service
public class DemandServiceImpl implements DemandService {
	
	@Autowired
	DemandDao demandImpl;
	
	@Autowired
	UserDao userImpl;
	
	VCodeSender phoneCode = VCodeSender.getInstance("N1330628", "t7NYh90uB");

	@Override
	public PageResult<RDemandBean> getList(int page, int pageSize, String uid, String content) {
		PageResult<RDemandBean> result = new PageResult<RDemandBean>();
		UserBean user = userImpl.selectOne(uid);
		if (user == null) {
			throw new ServiceException("object_is_not_exist", "用户");
		}
		int count = demandImpl.selectCount(user.getCateCode(), content);
		List<DemandBean> resList = demandImpl.selectList(page, pageSize, user.getCateCode(), content);
		List<RDemandBean> resData = new ArrayList<>();
		for(DemandBean d : resList) {
			RDemandBean rc = new RDemandBean();
			rc.setName(d.getName());
			rc.setPhone(d.getPhone());
			rc.setDid(d.getDid());
			rc.setQuickCenter(DemandStrToArry(d.getQuickCenter()));
			rc.setIndustrialDesign(DemandStrToArry(d.getIndustrialDesign()));
			rc.setConceptualDesign(DemandStrToArry(d.getConceptualDesign()));
			rc.setStandardCertification(DemandStrToArry(d.getStandardCertification()));
			rc.setMidSmallBatch(DemandStrToArry(d.getMidSmallBatch()));
			rc.setTechServices(DemandStrToArry(d.getTechServices()));
			rc.setProductMarketing(DemandStrToArry(d.getProductMarketing()));
			rc.setCreateTime(d.getCreateTime().getTime());
			rc.setHandleState(d.getHandleState());
			
			ParkUserBean pu = d.getUser();
			RParkUserBean ruser = new RParkUserBean();
			ruser.setAccount(pu.getAccount());
			ruser.setUserName(pu.getNickName());
			ruser.setHeadIcon(pu.getHeadIcon());
			ruser.setPark(pu.getPark());
			rc.setUser(ruser);
			resData.add(rc);
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}
	
	private String[] DemandStrToArry(String demands) {
		if(StringUtil.isEmpty(demands)) {
			return null;
		}
		demands = demands.replaceAll("'", "");
 		demands = demands.substring(1,demands.length()-1);
		return demands.split(",");
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean handle(String did) {
		boolean flag = false;
		DemandBean deman = demandImpl.selectOneById(did);
		if(deman == null) {
			throw new ServiceException("object_is_not_exist", "需求");
		}
		flag = demandImpl.updateHandleState(did, 1) > 0;
		if(!flag ) {
			throw new ServiceException("error", "需求处理");
		}
		if (StringUtil.isMobileNO(deman.getPhone())) {//
			String msg = StringConstants.getInstance().getString("msg_handle_send");
			flag = phoneCode.send(deman.getPhone(), msg, null);
		}
		return flag;
	}


}
